package com.practices.activity;

import java.io.InputStream;

import com.practice.R;
import com.practices.util.HttpUtil;
import com.practices.util.Util;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
	private String imageUrl = "http://h.hiphotos.baidu.com/album/w%3D2048/sign=fc71e566cefc1e17fdbf8b317ea8f703/0bd162d9f2d3572cae07ceac8b13632763d0c3dd.jpg";
	private String htmlUrl = "http://www.baidu.com";
	private String mp3Downer = "http://hugemaxiao.co/COFFdD0xMzY2ODU0MDUwJmk9MjE4LjE3LjE2Mi4xMzMmdT1Tb25ncy92MS9mYWludFFDL2ViLzg0N2YxNzE1YTk3OWYxYWMxZTA5NTdhNDYzZmNjNGViLm1wMyZtPTA4MDQ3ZmE0NjUwOTYyYzJiM2U0OGY5NTFhOWRmNDJlJnY9ZG93biZuPUdlbnRsZW1hbiZzPVBTWSUyMCZwPXM=.mp3";
	private Bitmap bitmap;

	private TextView mTextView;
	private ImageView mImageView;
	private ProgressDialog dialog;
	private Button btn_down, btn_anim;

	private Intent mIntent;

	private Handler handler = new MyHandler();
	private Handler imageHandler = new MyHandler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		initView();
		PackageManager pm = getPackageManager();
		// Canvas canvas=new Canvas();
		// Rect rect=new Rect(0, 0, 55, 55);
		// canvas.clipRect(rect)
		// canvas.d
		// ResolveInfo

	}

	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				mTextView.setText(msg.getData().getString("getbaidu"));
				Util.dismissshowProgressDialogWithText();
				break;
			case 2:
				mImageView.setImageBitmap(bitmap);
				break;
			default:
				break;
			}
		}
	}

	private void initView() {
		mTextView = (TextView) findViewById(R.id.tv_post);
		mImageView = (ImageView) findViewById(R.id.iv_down);
		btn_down = (Button) findViewById(R.id.btn_anim);
		btn_down.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(MainActivity.this, AnimationPractioces.class);
				startActivity(intent);
			}
		});
		btn_down = (Button) findViewById(R.id.btn_dian);
		btn_down.setText("down");
		btn_down.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mIntent = new Intent();
				mIntent.setClass(MainActivity.this, DownPicActiviy2.class);
				startActivity(mIntent);
			}
		});

		Util.showProgressDialogWithText(MainActivity.this, "prompt", "londing");

		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				String result = HttpUtil.getDataForStr(htmlUrl);
				Message msg = handler.obtainMessage();
				msg.what = 1;
				// 返回数据用bundle 存放于 Message 中
				Bundle bundle = new Bundle();
				bundle.putString("getbaidu", result);
				msg.setData(bundle);

				// 向Handler 发送消息
				handler.sendMessage(msg);
				InputStream is = HttpUtil.getDataForIO(imageUrl);
				bitmap = BitmapFactory.decodeStream(is);
				imageHandler.sendEmptyMessage(2);
			}
		});
		thread.start();
		btn_down.setOnTouchListener(touchLight);
		// mTextView.setText(result);
	}

	public final static OnTouchListener touchLight = new OnTouchListener() {

		public final float[] BT_SELECTED = new float[] { 1, 0, 0, 0, 50, 0, 1, 0, 0, 50, 0, 0, 1, 0, 50, 0, 0, 0, 1, 0 };
		public final float[] BT_NOT_SELECTED = new float[] { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 };

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));
				v.setBackgroundDrawable(v.getBackground());
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));
				v.setBackgroundDrawable(v.getBackground());
			}
			return false;
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(menu.NONE, 1, 1, "Grid");
		menu.add(menu.NONE, 1, 2, "getCacheDir");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 1:
			item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
				@Override
				public boolean onMenuItemClick(MenuItem item) {
					Intent intent = new Intent();
					intent.setClass(MainActivity.this, DownPicActiviy.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivity(intent);
					return true;
				}
			});
			break;
		case 2:
			item.setOnMenuItemClickListener(new OnMenuItemClickListener() {

				@Override
				public boolean onMenuItemClick(MenuItem item) {
					Intent intent = new Intent();
					intent.setClass(MainActivity.this, TestActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivity(intent);

					return true;
				}
			});

			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void startActivityForResult(Intent intent, int requestCode) {
		super.startActivityForResult(intent, requestCode);

	}

}
